package com.elite.groovy.dev;

import com.elite.groovy.kh.KHUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @Title: 法大大数据修复
 */
public class Dev_CheckSysTables {
    private static final Log log = LogFactory.getLog(Dev_CheckSysTables.class);
    private static JdbcTemplate jt = new JdbcTemplate(TxConfig.getDruidDataSource());

    public static void main(String[] args) throws Exception {

        batchContractSignFix();

    }

    // Fix-获取所有表
    public static void batchContractSignFix() throws InterruptedException, IOException {
        String dealSQL = "Select TABLE_NAME from ngs_sys_tables where remark is null or remark = ''";
        List<Map<String, Object>> selectCus = jt.queryForList(dealSQL);
        log.debug("Fix-获取所有表 :" + selectCus.size());

        // 处理节点数据
        for (Map<String, Object> CusMap : selectCus) {
            String table_Name = CusMap.get("TABLE_NAME").toString();
            String line_Str = isStrExist(table_Name);

            if (!KHUtil.isNull(line_Str)) {

                doDataFix(table_Name, line_Str);
            } else {

                doDataFix(table_Name, "未找到");
            }

        }

    }

    // Fix-临时导入数据检查
    public static void doDataFix(String table_Name, String line_Str) {
        String updateSQL = "Update Ngs_Sys_Tables Set Remark = ? Where Table_Name = ?";
        jt.update(updateSQL, line_Str, table_Name);

    }

    public static String isStrExist(String str) throws IOException {
        String fileName = "D:\\GeneralServer\\Apache-Tomcat-9.0.93\\webapps\\ngs\\WEB-INF\\classes\\SystemSql.properties";
        //加载本地文件
        File file = new File(fileName);
        //字节流到字符流
        InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8"); //编码格式
        //字符输入流中读取文本并缓冲字符
        BufferedReader bufferedReader = new BufferedReader(read);
        String line = null;
        int line_Num = 0;
        while ((line = bufferedReader.readLine()) != null) {
            line_Num++;
            if (line.startsWith("#")) {
                continue;
            }
            //指定字符串判断处
            if (line.toUpperCase().contains(str.toUpperCase())) {
                System.out.println("Exist " + line);
                return line;
            }
        }
        bufferedReader.close();

        return "";
    }


}